﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace YJ.Data.MSSQL
{
    public class HastenLog : YJ.Data.Interface.IHastenLog
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public HastenLog()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.HastenLog实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.HastenLog model)
        {
            string sql = @"INSERT INTO HastenLog
				(ID,OthersParams,Users,Types,Contents,SendUser,SendUserName,SendTime) 
				VALUES(@ID,@OthersParams,@Users,@Types,@Contents,@SendUser,@SendUserName,@SendTime)";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID },
				new SqlParameter("@OthersParams", SqlDbType.VarChar, 5000){ Value = model.OthersParams },
				new SqlParameter("@Users", SqlDbType.VarChar, 5000){ Value = model.Users },
				new SqlParameter("@Types", SqlDbType.VarChar, 500){ Value = model.Types },
				new SqlParameter("@Contents", SqlDbType.NVarChar, 1000){ Value = model.Contents },
				new SqlParameter("@SendUser", SqlDbType.UniqueIdentifier, -1){ Value = model.SendUser },
				new SqlParameter("@SendUserName", SqlDbType.NVarChar, 100){ Value = model.SendUserName },
				new SqlParameter("@SendTime", SqlDbType.DateTime, 8){ Value = model.SendTime }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.HastenLog实体类</param>
        public int Update(YJ.Data.Model.HastenLog model)
        {
            string sql = @"UPDATE HastenLog SET 
				OthersParams=@OthersParams,Users=@Users,Types=@Types,Contents=@Contents,SendUser=@SendUser,SendUserName=@SendUserName,SendTime=@SendTime
				WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@OthersParams", SqlDbType.VarChar, 5000){ Value = model.OthersParams },
				new SqlParameter("@Users", SqlDbType.VarChar, 5000){ Value = model.Users },
				new SqlParameter("@Types", SqlDbType.VarChar, 500){ Value = model.Types },
				new SqlParameter("@Contents", SqlDbType.NVarChar, 1000){ Value = model.Contents },
				new SqlParameter("@SendUser", SqlDbType.UniqueIdentifier, -1){ Value = model.SendUser },
				new SqlParameter("@SendUserName", SqlDbType.NVarChar, 100){ Value = model.SendUserName },
				new SqlParameter("@SendTime", SqlDbType.DateTime, 8){ Value = model.SendTime },
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM HastenLog WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.HastenLog> DataReaderToList(SqlDataReader dataReader)
        {
            List<YJ.Data.Model.HastenLog> List = new List<YJ.Data.Model.HastenLog>();
            YJ.Data.Model.HastenLog model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.HastenLog();
                model.ID = dataReader.GetGuid(0);
                model.OthersParams = dataReader.GetString(1);
                model.Users = dataReader.GetString(2);
                model.Types = dataReader.GetString(3);
                model.Contents = dataReader.GetString(4);
                model.SendUser = dataReader.GetGuid(5);
                model.SendUserName = dataReader.GetString(6);
                model.SendTime = dataReader.GetDateTime(7);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.HastenLog> GetAll()
        {
            string sql = "SELECT * FROM HastenLog";
            SqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.HastenLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM HastenLog";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.HastenLog Get(Guid id)
        {
            string sql = "SELECT * FROM HastenLog WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.HastenLog> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }
    }
}